package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ImbiConnection {
	private static String url = "jdbc:postgresql://imbi.ld.ttu.ee/";
	private static String driver = "org.postgresql.Driver";
	private static String user = "t073857";
	private static String password = "3YbvWKLW";
	private static String db = "t073857";
	private static Connection con;
	
	private static AddressDAO addressDAO;
	private static AddressTypeDAO addressTypeDAO;
	private static ContactDAO contactDAO;
	private static ContactTypeDAO contactTypeDAO;
	private static CustomerDAO customerDAO;
	private static EmployeeDAO employeeDAO;
	private static EmployeeRoleDAO employeeRoleDAO;
	private static EmployeeRoleTypeDAO employeeRoleTypeDAO;
	private static EnterpriseDAO enterpriseDAO;
	private static EnterprisePersonRelationDAO enterprisePersonRelationDAO;
	private static EntPerRelationTypeDAO entPerRelationTypeDAO;
	private static PersonDAO personDAO;	
	private static SubjectAttributeDAO subjectAttributeDAO;
	private static SubjectAttributeTypeDAO subjectAttributeTypeDAO;
	private static SubjectTypeDAO subjectTypeDAO;
	private static UserAccountDAO userAccountDAO;
	
	static{
		addressDAO = new AddressDAO();
		addressTypeDAO = new AddressTypeDAO();
		contactDAO = new ContactDAO();
		contactTypeDAO = new ContactTypeDAO();
		customerDAO = new CustomerDAO();
		employeeDAO = new EmployeeDAO();
		employeeRoleDAO = new EmployeeRoleDAO();
		employeeRoleTypeDAO = new EmployeeRoleTypeDAO();
		enterpriseDAO = new EnterpriseDAO();
		enterprisePersonRelationDAO = new EnterprisePersonRelationDAO();
		entPerRelationTypeDAO = new EntPerRelationTypeDAO();
		personDAO = new PersonDAO();		
		subjectAttributeDAO = new SubjectAttributeDAO();
		subjectAttributeTypeDAO = new SubjectAttributeTypeDAO();
		subjectTypeDAO = new SubjectTypeDAO();
		userAccountDAO = new UserAccountDAO();		
	}
	
	public static Connection getConnection() throws SQLException{
		if(con == null || con.isClosed())
			try {
				Class.forName(driver).newInstance();
				try{	
					con = DriverManager.getConnection(url+db, user, password);	
				} catch (SQLException s){
					s.printStackTrace();
				}						
			} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {				
				e.printStackTrace();
				return null;
			}
		return con;
	}	

	public static AddressDAO getAddressDAO() {
		return addressDAO;
	}

	public static AddressTypeDAO getAddressTypeDAO() {
		return addressTypeDAO;
	}

	public static ContactDAO getContactDAO() {
		return contactDAO;
	}

	public static ContactTypeDAO getContactTypeDAO() {
		return contactTypeDAO;
	}

	public static CustomerDAO getCustomerDAO() {
		return customerDAO;
	}

	public static EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public static EmployeeRoleDAO getEmployeeRoleDAO() {
		return employeeRoleDAO;
	}

	public static EmployeeRoleTypeDAO getEmployeeRoleTypeDAO() {
		return employeeRoleTypeDAO;
	}

	public static EnterpriseDAO getEnterpriseDAO() {
		return enterpriseDAO;
	}

	public static EnterprisePersonRelationDAO getEnterprisePersonRelationDAO() {
		return enterprisePersonRelationDAO;
	}

	public static EntPerRelationTypeDAO getEntPerRelationTypeDAO() {
		return entPerRelationTypeDAO;
	}

	public static PersonDAO getPersonDAO() {
		return personDAO;
	}
		
	public static SubjectAttributeDAO getSubjectAttributeDAO() {
		return subjectAttributeDAO;
	}

	public static SubjectAttributeTypeDAO getSubjectAttributeTypeDAO() {
		return subjectAttributeTypeDAO;
	}

	public static SubjectTypeDAO getSubjectTypeDAO() {
		return subjectTypeDAO;
	}

	public static UserAccountDAO getUserAccountDAO() {
		return userAccountDAO;
	}
	
}
